package com.nebula.tlggoods.service.impl;

import com.nebula.tlggoods.entity.Carts;
import com.nebula.tlggoods.mapper.CartsMapper;
import com.nebula.tlggoods.service.CartsService;
import com.nebula.tlggoods.tools.Record;
import com.nebula.tlggoods.tools.Status;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 购物车实现类
 */
@Service
public class CartsServiceImpl implements CartsService {

    @Resource
    private CartsMapper cartsMapper;

    /**
     * 加入购物车
     *
     * @param carts
     * @return
     */
    @Transactional
    @Override
    public Record addCarts(Carts carts) {
        Record record = new Record();
        int count = 0;
        try {
            count = cartsMapper.addCarts(carts);
        } catch (Exception e) {
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("加入购物车异常!!!");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.SAVE_SUCCESS);
            record.setMsg("加入购物车成功");
        } else {
            record.setStatus(Status.SAVE_FALIED);
            record.setMsg("加入购物车失败!!");
        }
        return record;
    }


    /**
     * 查询购物车商品
     *
     * @param carts
     * @return
     */
    @Override
    public Record findCartsGoods(Carts carts) {
        Record record = new Record();
        int count = 0;
        try {
            count = cartsMapper.findCartsGoods(carts);
        } catch (Exception e) {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询购物车商品异常!!!");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.SELECT_SUCCESS);
            record.setMsg("查询购物车商品成功");
        } else {
            record.setStatus(Status.SELECT_FALIED);
            record.setMsg("查询购物车商品失败!!");
        }
        return record;
    }

    /**
     * 修改购物车商品数量
     *
     * @param carts
     * @return
     */
    @Transactional
    @Override
    public Record modifyCartsGoodsQuantity(Carts carts) {
        Record record = new Record();
        int count = 0;
        try {
            count = cartsMapper.modifyCartsGoodsQuantity(carts);
        } catch (Exception e) {
            record.setStatus(Status.UPDATE_SUCCESS);
            record.setMsg("购物车商品数量修改异常!!!");
            e.printStackTrace();
            return record;
        }
        if (count > 0) {
            record.setStatus(Status.UPDATE_FALIED);
            record.setMsg("购物车商品数量修改成功");
        } else {
            record.setStatus(Status.UPDATE_SUCCESS);
            record.setMsg("购物车商品数量修改失败!!");
        }
        return record;
    }
}
